package com.example.demo.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.example.demo.entity.Competition;
import com.example.demo.mapper.CompetitionAdMapper;


@Service
public class AdMainService {
	@Autowired
	CompetitionAdMapper competitionAdMapper;
	//事务回滚
	//查找所有比赛信息
	@Transactional
	public List<Competition> getAllCompetitionInfoAd() {
		try {
		return competitionAdMapper.getAllCompetitionInfoAd();
	}
		catch(Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return competitionAdMapper.getAllCompetitionInfoAd();
	}
	
	//审批赛事，修改赛事状态为已同意
	@Transactional
	public boolean approveCompetition(String OrganizersName,String CompetitionName,String CompetitionType,String CompetitionDate,String CompetitionRemarks,String CompetitionPrice)
	{
		try {
		int i=0;
		i=competitionAdMapper.approveCompetition(OrganizersName,CompetitionName,CompetitionType,CompetitionDate,CompetitionRemarks,CompetitionPrice);
		if(i==1)
			return true;
		else
			return false;
	}
		catch(Exception e){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return true;
	}
	
	//审批赛事，修改赛事状态为拒绝
	@Transactional
	public boolean refuseCompetition(String OrganizersName,String CompetitionName,String CompetitionType,String CompetitionDate,String CompetitionRemarks,String CompetitionPrice)
	{
		try {
		int i=0;
		i=competitionAdMapper.refuseCompetition(OrganizersName,CompetitionName,CompetitionType,CompetitionDate,CompetitionRemarks,CompetitionPrice);
		if(i==1)
			return true;
		else
			return false;
	}
		catch(Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return true;
	}
	
	//查找审批记录
	@Transactional
	public List<Competition> queryByDate(String date1,String date2) throws  java.text.ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date dateBegin=sdf.parse(date1);
		Date dateEnd=sdf.parse(date2);
		try {
		return competitionAdMapper.queryByDate(dateBegin,dateEnd);	
		}
		catch(Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return competitionAdMapper.queryByDate(dateBegin,dateEnd);
	}
	
}

